package com.example.moban.base;

import android.util.Log;

/**
 * 需要懒加载的Fragment的基类
 *
 * @author yql
 * @date
 */

public abstract class BaseLazyFragment extends BaseFragment {

    protected boolean isViewCreated;
    protected boolean isUIVisible;
    protected boolean isDataLoaded;
    protected boolean forceUpdate;


    @Override
    public void onResume() {
        super.onResume();
        isViewCreated = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: 3");
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    private void lazyLoad() {
        if (isViewCreated && isUIVisible && (!isDataLoaded || forceUpdate)) {
            loadData();
            isDataLoaded = true;
        }
    }

    /**
     * 懒加载调用
     */
    protected abstract void loadData();
}
